<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="描述" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入描述" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="表名" prop="tableName">
        <el-input v-model="queryParams.tableName" placeholder="请输入表名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['vote:field-dynamics-config:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['vote:field-dynamics-config:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="描述" align="center" prop="title" />
      <el-table-column label="表名" align="center" prop="tableName" />
      <!-- <el-table-column label="字段列表" align="center" prop="fieldList" /> -->
      <!-- <el-table-column label="需要显示的字段" align="center" prop="showFieldList" /> -->
      <!-- <el-table-column label="排序字段及方式" align="center" prop="orderList" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['vote:field-dynamics-config:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['vote:field-dynamics-config:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="描述" prop="title">
          <el-input v-model="form.title" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="表名" prop="tableName">
          <el-input v-model="form.tableName" placeholder="请输入表名" />
        </el-form-item>
        <el-form-item label="显示字段" prop="showFieldList">
          <el-transfer filterable :titles="['未选择字段', '已选择字段']" v-model="checkedShowField" :data="fieldShowList"></el-transfer>
        </el-form-item>
        <el-form-item label="必查字段" prop="mustFieldList">
          <el-transfer filterable :titles="['未选择字段', '已选择字段']" v-model="checkedMustField" :data="fieldMustList"></el-transfer>
        </el-form-item>
        <!-- <el-form-item label="字段列表" prop="fieldList">
          <el-input v-model="form.fieldList" placeholder="请输入字段列表" />
        </el-form-item>
        <el-form-item label="需要显示的字段" prop="showFieldList">
          <el-input v-model="form.showFieldList" placeholder="请输入需要显示的字段" />
        </el-form-item>
        <el-form-item label="排序字段及方式" prop="orderList">
          <el-input v-model="form.orderList" placeholder="请输入排序字段及方式" />
        
        </el-form-item> -->

        <el-row v-for="(item, index) in form.orderFrontList" :key="index">
           <el-col :span="11">
             <el-form-item  label="排序字段" :prop="'orderFrontList.' + index + '.fieldName'" :rules="[{ required: true, message: '请选择排序字段', trigger: 'blur' }]" >
                 <el-select v-model="item.fieldName" filterable placeholder="请输入关键词" @change="updateFieldList($event)">
                 <el-option
                   v-for="field in fieldAllList"
                   :disabled="field.disabled"
                   :key="field.fieldName"
                   :label="field.desc"
                   :value="field.fieldName">
                 </el-option>
               </el-select>
               </el-form-item>
           </el-col>
           <el-col :span="11">
               <el-form-item label="是否升序"  :prop="'orderFrontList.' + index + '.isAsc'" :rules="[{ required: true, message: '请选择是否升序', trigger: 'blur' }]">
                 <el-select v-model="item.isAsc" filterable>
                   <el-option label="升序" :value="true"></el-option>
                   <el-option label="降序" :value="false"></el-option>
                 </el-select>
               </el-form-item>
           </el-col>
           <el-col :span="2">
               <i @click="delSection(index)" class="el-icon-delete btn-delete" ></i>
           </el-col>
       </el-row>

       <el-row>
            <el-col :span="24" style="text-align: right;">
                <el-form-item label="" >
                    <el-button  @click="newSection()" type="primary" plain icon="el-icon-plus" style="display: block;width: 100%;">添加排序</el-button>
                </el-form-item>
            </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createFieldDynamicsConfig, updateFieldDynamicsConfig, deleteFieldDynamicsConfig, getFieldDynamicsConfig, getFieldDynamicsConfigPage, exportFieldDynamicsConfigExcel } from "@/api/vote/fieldDynamicsConfig";

export default {
  name: "FieldDynamicsConfig",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表动态配置字段展示列表
      list: [],
      // 弹出层标题
      title: "",

      fieldAllList: [],
      
      fieldShowList: [],
      fieldMustList: [],

      checkedShowField: [],
      checkedMustField: [],
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        title: null,
        tableName: null,
        fieldList: null,
        showFieldList: null,
        orderList: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getFieldDynamicsConfigPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        tableName: undefined,
        fieldList: undefined,
        showFieldList: undefined,
        orderList: undefined,
        mustFieldList: undefined
      };
      this.resetForm("form");
      this.checkedMustField=[];
      this.checkedShowField=[];
      this.fieldShowList=[];
      this.fieldMustList=[];
      this.fieldAllList=[];
    },


    updateFieldList(val){
      for(let field of this.fieldAllList){
        if(val == field.fieldName){
          field.disabled = true;
        }
      }
    },

    newSection () {
      this.form.orderFrontList.push({
        fieldName: undefined,
        isAsc: undefined
      })
    },
    delSection (index) {
      this.form.orderFrontList.splice(index,1)
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加表动态配置字段展示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getFieldDynamicsConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改表动态配置字段展示";
        this.form.fieldInfoList.forEach((item, index) => {
          if(this.form.showFieldList.includes(item.fieldName)){
            this.checkedShowField.push(item.fieldName);
          }
          this.fieldShowList.push({
              key: item.fieldName,
              label: item.desc
            });

          let disabled = false;
          for(let order of this.form.orderFrontList){
            if(order.fieldName == item.fieldName){
              disabled = true;
            }
          }
          this.fieldAllList.push({fieldName:item.fieldName,desc:item.fieldName+item.desc,disabled:disabled})
        });


        this.form.fieldInfoList.forEach((item, index) => {
          if(this.form.mustFieldList.includes(item.fieldName)){
            this.checkedMustField.push(item.fieldName)
          }
          this.fieldMustList.push({
              key: item.fieldName,
              label: item.desc
            });
        });


      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        this.form.showFieldList = this.checkedShowField.toString();
        this.form.mustFieldList = this.checkedMustField.toString();
        // 修改的提交
        if (this.form.id != null) {
          updateFieldDynamicsConfig(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createFieldDynamicsConfig(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除表动态配置字段展示编号为"' + id + '"的数据项?').then(function() {
          return deleteFieldDynamicsConfig(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有表动态配置字段展示数据项?').then(() => {
          this.exportLoading = true;
          return exportFieldDynamicsConfigExcel(params);
        }).then(response => {
          this.$download.excel(response, '表动态配置字段展示.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
